﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace MX.Core.Encryption
{
    public static class DesHelper
    {
        static byte[] keys = new byte[] { 0x36, 0x66, 0x33, 0x38, 0x66, 0x33, 0x36, 0x30 };

        ///<summary><![CDATA[字符串DES加密函数]]></summary>
        ///<param name="str"><![CDATA[被加密字符串 ]]></param>
        ///<param name="key"><![CDATA[密钥 ]]></param> 
        ///<returns><![CDATA[加密后字符串]]></returns>   
        public static byte[] Encrypt(byte[] bytes)
        {

            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            provider.Key = keys;
            provider.IV = keys;
            using (MemoryStream stream = new MemoryStream())
            using (CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write))
            {
                stream2.Write(bytes, 0, bytes.Length);
                stream2.FlushFinalBlock();
                return stream.ToArray();
            }

        }
        ///<summary><![CDATA[字符串DES解密函数]]></summary>
        ///<param name="str"><![CDATA[被解密字符串 ]]></param>
        ///<param name="key"><![CDATA[密钥 ]]></param> 
        ///<returns><![CDATA[解密后字符串]]></returns>   
        public static byte[] Decrypt(byte[] bytes)
        {
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            provider.Key = keys;
            provider.IV = keys;
            using (MemoryStream stream = new MemoryStream())
            using (CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write))
            {
                stream2.Write(bytes, 0, bytes.Length);
                stream2.FlushFinalBlock();
                return stream.ToArray();
            }
        }
    }
}
